using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;

using Framework;

namespace SchemaMonitor
{
    [Serializable()]
    public partial class CPublish : CBaseDynamic, IComparable<CPublish>
    {
        #region Constructors
        //Public (Copy Constructor)
        public CPublish(CPublish original, CDataSrc target)
        {
            //Database Connection
            m_dataSrc = target;

            //Copy fields
            _publishToken = original.PublishToken;
            _publishStarted = original.PublishStarted;
            _publishFinished = original.PublishFinished;
            _publishTarget = original.PublishTarget;
            _sectionTotalCount = original.SectionTotalCount;
            _sectionCompleteCount = original.SectionCompleteCount;
            _blurbTotalLength = original.BlurbTotalLength;
            _chaptersTotalCount = original.ChaptersTotalCount;
            _wordsTotalCount = original.WordsTotalCount;
            _wordsCompleteCount = original.WordsCompleteCount;
            _chaptersCompleteCount = original.ChaptersCompleteCount;
            _chapterUploadTimeMs = original.ChapterUploadTimeMs;
            _imagesTotalCount = original.ImagesTotalCount;
            _imagesTotalFileSize = original.ImagesTotalFileSize;
            _imagesCompleteCount = original.ImagesCompleteCount;
            _imagesUploadTimeMs = original.ImagesUploadTimeMs;
            _chapterCommentsTotalCount = original.ChapterCommentsTotalCount;
            _chapterCommentUploadTimeMs = original.ChapterCommentUploadTimeMs;
            _imageCommentsTotalCount = original.ImageCommentsTotalCount;
            _imageCommentsCompletedCount = original.ImageCommentsCompletedCount;
            _pictureCommentsTotalMs = original.PictureCommentsTotalMs;
            _publishBookId = original.PublishBookId;
        }

        //Protected (Datareader/Dataset)
        protected CPublish(CDataSrc dataSrc, IDataReader dr) : base(dataSrc, dr) {}
        protected CPublish(CDataSrc dataSrc, DataRow     dr) : base(dataSrc, dr) {}
        #endregion

        #region Default Values
        protected override void InitValues_Auto()
        {
            //Null-Equivalent values (except String.Empty, as nulls tend to be inconvenient for string types)
            _publishId = int.MinValue;
            _publishBookId = int.MinValue;
            _publishToken = string.Empty;
            _publishStarted = DateTime.MinValue;
            _publishFinished = DateTime.MinValue;
            _publishTarget = DateTime.MinValue;
        }
        #endregion

        #region Members
        protected int _publishId;
        protected string _publishToken;
        protected DateTime _publishStarted;
        protected DateTime _publishFinished;
        protected DateTime _publishTarget;
        protected int _sectionTotalCount;
        protected int _sectionCompleteCount;
        protected long _blurbTotalLength;
        protected int _chaptersTotalCount;
        protected int _wordsTotalCount;
        protected long _wordsCompleteCount;
        protected int _chaptersCompleteCount;
        protected long _chapterUploadTimeMs;
        protected int _imagesTotalCount;
        protected long _imagesTotalFileSize;
        protected int _imagesCompleteCount;
        protected long _imagesUploadTimeMs;
        protected int _chapterCommentsTotalCount;
        protected long _chapterCommentUploadTimeMs;
        protected int _imageCommentsTotalCount;
        protected int _imageCommentsCompletedCount;
        protected long _pictureCommentsTotalMs;
        protected int _publishBookId;
        #endregion

        #region Properties - Column Values
        //Primary Key Column (ReadOnly)
        public int PublishId    {   get { return _publishId;   } }

        //Table Columns (Read/Write)
        public int PublishBookId    {   get { return _publishBookId; } set { _publishBookId = value; }    }
        public string PublishToken    {   get { return _publishToken; } set { _publishToken = value; }    }
        public DateTime PublishStarted    {   get { return _publishStarted; } set { _publishStarted = value; }    }
        public DateTime PublishFinished    {   get { return _publishFinished; } set { _publishFinished = value; }    }
        public DateTime PublishTarget    {   get { return _publishTarget; } set { _publishTarget = value; }    }

        //View Columns (ReadOnly)
        public int SectionTotalCount    {   get { return _sectionTotalCount;   } }
        public int SectionCompleteCount    {   get { return _sectionCompleteCount;   } }
        public long BlurbTotalLength    {   get { return _blurbTotalLength;   } }
        public int ChaptersTotalCount    {   get { return _chaptersTotalCount;   } }
        public int WordsTotalCount    {   get { return _wordsTotalCount;   } }
        public long WordsCompleteCount    {   get { return _wordsCompleteCount;   } }
        public int ChaptersCompleteCount    {   get { return _chaptersCompleteCount;   } }
        public long ChapterUploadTimeMs    {   get { return _chapterUploadTimeMs;   } }
        public int ImagesTotalCount    {   get { return _imagesTotalCount;   } }
        public long ImagesTotalFileSize    {   get { return _imagesTotalFileSize;   } }
        public int ImagesCompleteCount    {   get { return _imagesCompleteCount;   } }
        public long ImagesUploadTimeMs    {   get { return _imagesUploadTimeMs;   } }
        public int ChapterCommentsTotalCount    {   get { return _chapterCommentsTotalCount;   } }
        public long ChapterCommentUploadTimeMs    {   get { return _chapterCommentUploadTimeMs;   } }
        public int ImageCommentsTotalCount    {   get { return _imageCommentsTotalCount;   } }
        public int ImageCommentsCompletedCount    {   get { return _imageCommentsCompletedCount;   } }
        public long PictureCommentsTotalMs    {   get { return _pictureCommentsTotalMs;   } }

        #endregion

        #region MustOverride Methods
        //Schema Information
        public const string TABLE_NAME      = "Publish";
        public const string VIEW_NAME       = "vwPublish";         //Used to override this.ViewName { get }
        public const string ORDER_BY_COLS   = "PublishId";   //See the CompareTo method below (Sql-based sorting should match In-Memory sorting)
        public const string SORTING_COLUMN  = "";
        public override string TableName {    get { return TABLE_NAME; } }
        protected override string ViewName {    get {   return VIEW_NAME;    }    }
        protected override string OrderByColumns {    get { return ORDER_BY_COLS; } }

        //CompareTo Interface (Default Sort Order)
        public int CompareTo(CPublish other) {   return this.PublishId.CompareTo(other.PublishId) ;  }

        //Primary Key Information
        public const string PRIMARY_KEY_NAME = "PublishId";
        protected override bool InsertPrimaryKey {  get { return false;    }   }
        protected override string PrimaryKeyName {  get { return PRIMARY_KEY_NAME;    }    }
        protected override object PrimaryKeyValue 
        {
              get { return _publishId; }
              set { _publishId = (int)value; }
        }

        //Factory Methods - Object
        protected override CBase MakeFrom(DataRow row)      {   return new CPublish(this.DataSrc, row);  }
        protected override CBase MakeFrom(IDataReader dr)   {   return new CPublish(this.DataSrc, dr);   }

        //Factory Methods - List
        protected override IList MakeList()                 {   return new CPublishList();               }
        protected override IList MakeList(int capacity)     {   return new CPublishList(capacity);       }

        //Convert from ADO to .Net
        protected override void ReadColumns(IDataReader dr)
        {
            _publishId = CAdoData.GetInt(dr, "PublishId");
            _publishToken = CAdoData.GetStr(dr, "PublishToken");
            _publishStarted = CAdoData.GetDate(dr, "PublishStarted");
            _publishFinished = CAdoData.GetDate(dr, "PublishFinished");
            _publishTarget = CAdoData.GetDate(dr, "PublishTarget");
            _sectionTotalCount = CAdoData.GetInt(dr, "SectionTotalCount");
            _sectionCompleteCount = CAdoData.GetInt(dr, "sectionCompleteCount");
            _blurbTotalLength = CAdoData.GetLong(dr, "BlurbTotalLength");
            _chaptersTotalCount = CAdoData.GetInt(dr, "ChaptersTotalCount");
            _wordsTotalCount = CAdoData.GetInt(dr, "WordsTotalCount");
            _wordsCompleteCount = CAdoData.GetLong(dr, "WordsCompleteCount");
            _chaptersCompleteCount = CAdoData.GetInt(dr, "ChaptersCompleteCount");
            _chapterUploadTimeMs = CAdoData.GetLong(dr, "ChapterUploadTimeMs");
            _imagesTotalCount = CAdoData.GetInt(dr, "ImagesTotalCount");
            _imagesTotalFileSize = CAdoData.GetLong(dr, "ImagesTotalFileSize");
            _imagesCompleteCount = CAdoData.GetInt(dr, "ImagesCompleteCount");
            _imagesUploadTimeMs = CAdoData.GetLong(dr, "ImagesUploadTimeMs");
            _chapterCommentsTotalCount = CAdoData.GetInt(dr, "ChapterCommentsTotalCount");
            _chapterCommentUploadTimeMs = CAdoData.GetLong(dr, "ChapterCommentUploadTimeMs");
            _imageCommentsTotalCount = CAdoData.GetInt(dr, "ImageCommentsTotalCount");
            _imageCommentsCompletedCount = CAdoData.GetInt(dr, "ImageCommentsCompletedCount");
            _pictureCommentsTotalMs = CAdoData.GetLong(dr, "PictureCommentsTotalMs");
            _publishBookId = CAdoData.GetInt(dr, "PublishBookId");
        }
        protected override void ReadColumns(DataRow dr)
        {
            _publishId = CAdoData.GetInt(dr, "PublishId");
            _publishToken = CAdoData.GetStr(dr, "PublishToken");
            _publishStarted = CAdoData.GetDate(dr, "PublishStarted");
            _publishFinished = CAdoData.GetDate(dr, "PublishFinished");
            _publishTarget = CAdoData.GetDate(dr, "PublishTarget");
            _sectionTotalCount = CAdoData.GetInt(dr, "SectionTotalCount");
            _sectionCompleteCount = CAdoData.GetInt(dr, "sectionCompleteCount");
            _blurbTotalLength = CAdoData.GetLong(dr, "BlurbTotalLength");
            _chaptersTotalCount = CAdoData.GetInt(dr, "ChaptersTotalCount");
            _wordsTotalCount = CAdoData.GetInt(dr, "WordsTotalCount");
            _wordsCompleteCount = CAdoData.GetLong(dr, "WordsCompleteCount");
            _chaptersCompleteCount = CAdoData.GetInt(dr, "ChaptersCompleteCount");
            _chapterUploadTimeMs = CAdoData.GetLong(dr, "ChapterUploadTimeMs");
            _imagesTotalCount = CAdoData.GetInt(dr, "ImagesTotalCount");
            _imagesTotalFileSize = CAdoData.GetLong(dr, "ImagesTotalFileSize");
            _imagesCompleteCount = CAdoData.GetInt(dr, "ImagesCompleteCount");
            _imagesUploadTimeMs = CAdoData.GetLong(dr, "ImagesUploadTimeMs");
            _chapterCommentsTotalCount = CAdoData.GetInt(dr, "ChapterCommentsTotalCount");
            _chapterCommentUploadTimeMs = CAdoData.GetLong(dr, "ChapterCommentUploadTimeMs");
            _imageCommentsTotalCount = CAdoData.GetInt(dr, "ImageCommentsTotalCount");
            _imageCommentsCompletedCount = CAdoData.GetInt(dr, "ImageCommentsCompletedCount");
            _pictureCommentsTotalMs = CAdoData.GetLong(dr, "PictureCommentsTotalMs");
            _publishBookId = CAdoData.GetInt(dr, "PublishBookId");
        }

        //Parameters for Insert/Update    
        protected override CNameValueList ColumnNameValues()
        {
            CNameValueList data = new CNameValueList();
            data.Add("PublishId", CAdoData.NullVal(_publishId));
            data.Add("PublishBookId", CAdoData.NullVal(_publishBookId));
            data.Add("PublishToken", CAdoData.NullVal(_publishToken));
            data.Add("PublishStarted", CAdoData.NullVal(_publishStarted));
            data.Add("PublishFinished", CAdoData.NullVal(_publishFinished));
            data.Add("PublishTarget", CAdoData.NullVal(_publishTarget));
            return data;
        }
        #endregion

        #region Queries - SelectAll/SelectWhere (inherited methods, cast only)
        //Select Queries - Non-Paged
        public new CPublishList SelectAll()                                                          {    return (CPublishList)base.SelectAll();                              }
        public new CPublishList SelectAll(string orderBy)                                            {    return (CPublishList)base.SelectAll(orderBy);                       }
        public new CPublishList SelectWhere(string colName, ESign sign, object colValue)             {    return (CPublishList)base.SelectWhere(colName, sign, colValue);     }
        public new CPublishList SelectWhere(CCriteria where)                                         {    return (CPublishList)base.SelectWhere(where);                       }
        public new CPublishList SelectWhere(CCriteriaList where)                                     {    return (CPublishList)base.SelectWhere(where);                       }
        public new CPublishList SelectWhere(CCriteriaList where, string tableOrJoin)                 {    return (CPublishList)base.SelectWhere(where, tableOrJoin);          }
        public new CPublishList SelectWhere(CCriteriaList where, string tableOrJoin, string orderBy) {    return (CPublishList)base.SelectWhere(where, tableOrJoin, orderBy); }
        [Obsolete("Arbitrary where-clause is not necessary portable or safe from sql injection attacks. Consider using the parameterised query interfaces")]
        public new CPublishList SelectWhere(string unsafeWhereClause)                                {    return (CPublishList)base.SelectWhere(unsafeWhereClause);           }
        public     CPublishList SelectById(int publishId)                              {    return (CPublishList)base.SelectById(publishId);       }
        public     CPublishList SelectByIds(List<int> ids)                        {    return (CPublishList)base.SelectByIds(ids);                         }

        //Select Queries - Paged
        public new CPublishList SelectAll(  CPagingInfo pi)                                              {    return (CPublishList)base.SelectAll(  pi);                              }
        public new CPublishList SelectWhere(CPagingInfo pi, string name, ESign sign, object value)       {    return (CPublishList)base.SelectWhere(pi, name, sign, value);           }
        public new CPublishList SelectWhere(CPagingInfo pi, CCriteria criteria)                          {    return (CPublishList)base.SelectWhere(pi, criteria);                    }
        public new CPublishList SelectWhere(CPagingInfo pi, CCriteriaList criteria)                      {    return (CPublishList)base.SelectWhere(pi, criteria);                    }
        public new CPublishList SelectWhere(CPagingInfo pi, CCriteriaList criteria, string viewOrJoin)   {    return (CPublishList)base.SelectWhere(pi, criteria, viewOrJoin);        }
        public     CPublishList SelectByIds(CPagingInfo pi, List<int> ids)            {    return (CPublishList)base.SelectByIds(pi, ids);                         }

        //Select Queries - Transactional
        public new CPublishList SelectAll(                                                                                           IDbTransaction tx)  {    return (CPublishList)base.SelectAll(tx);                                                     }
        public new CPublishList SelectAll(string orderBy,                                                                            IDbTransaction tx)  {    return (CPublishList)base.SelectAll(orderBy,                                         tx);    }
        public new CPublishList SelectWhere(string columnName,               object columnValue,                                     IDbTransaction tx)  {    return (CPublishList)base.SelectWhere(columnName,              columnValue,          tx);    }
        public new CPublishList SelectWhere(string columnName,   ESign sign, object columnValue,                                     IDbTransaction tx)  {    return (CPublishList)base.SelectWhere(columnName,    sign,     columnValue,          tx);    }
        public new CPublishList SelectWhere(                             CCriteria     criteria,                                     IDbTransaction tx)  {    return (CPublishList)base.SelectWhere(               criteria,                       tx);    }
        public new CPublishList SelectWhere(                             CCriteriaList criteria,                                     IDbTransaction tx)  {    return (CPublishList)base.SelectWhere(               criteria,                       tx);    }
        public new CPublishList SelectWhere(                             CCriteriaList criteria, string tableOrJoin,                 IDbTransaction tx)  {    return (CPublishList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public new CPublishList SelectWhere(                             CCriteriaList criteria, string tableOrJoin, string orderBy, IDbTransaction tx)  {    return (CPublishList)base.SelectWhere(               criteria, tableOrJoin,          tx);    }
        public     CPublishList SelectById(int publishId,                                                              IDbTransaction tx)  {    return (CPublishList)base.SelectById(publishId,                         tx);    }
        public     CPublishList SelectByIds(List<int> ids,                                                        IDbTransaction tx)  {    return (CPublishList)base.SelectByIds(ids,                                           tx);    }

        //Select Queries - Stored Procs
        public new CPublishList MakeList(string storedProcName,                             IDbTransaction txOrNull)    {   return (CPublishList)base.MakeList(storedProcName,             txOrNull);  }
        public new CPublishList MakeList(string storedProcName, object[] parameters,        IDbTransaction txOrNull)    {   return (CPublishList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CPublishList MakeList(string storedProcName, CNameValueList parameters,  IDbTransaction txOrNull)    {   return (CPublishList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CPublishList MakeList(string storedProcName, List<object> parameters,    IDbTransaction txOrNull)    {   return (CPublishList)base.MakeList(storedProcName, parameters, txOrNull);  }
        public new CPublishList MakeList(string storedProcName, int param1,                 IDbTransaction txOrNull)    {   return (CPublishList)base.MakeList(storedProcName, param1,     txOrNull);  }
        public new CPublishList MakeList(string storedProcName, string param1,              IDbTransaction txOrNull)    {   return (CPublishList)base.MakeList(storedProcName, param1,     txOrNull);  }

        //Query Results
        protected new CPublishList MakeList(DataSet              ds) { return (CPublishList)base.MakeList(ds);        }
        protected new CPublishList MakeList(DataTable            dt) { return (CPublishList)base.MakeList(dt);        }
        protected new CPublishList MakeList(DataRowCollection  rows) { return (CPublishList)base.MakeList(rows);      }
        protected new CPublishList MakeList(IDataReader          dr) { return (CPublishList)base.MakeList(dr);        }
        protected new CPublishList MakeList(object           drOrDs) { return (CPublishList)base.MakeList(drOrDs);    }        
        protected new CPublishList MakeList(byte[]             gzip) { return (CPublishList)base.MakeList(gzip);      }
        #endregion

        #region Queries - SelectBy[FK] (user-nominated fk/bool columns)
        //Non-Paged
        public CPublishList SelectByBookId(int publishBookId)    {    return SelectWhere(new CCriteriaList("PublishBookId", publishBookId));    }

        //Paged
        public CPublishList SelectByBookId(CPagingInfo pi, int publishBookId)    {    return SelectWhere(pi, new CCriteriaList("PublishBookId", publishBookId));    }

        //Count
        public int SelectCountByBookId(int publishBookId)   {   return SelectCount(new CCriteriaList("PublishBookId", publishBookId));     }

        //Transactional
        public CPublishList SelectByBookId(int publishBookId, IDbTransaction tx)    {    return SelectWhere(new CCriteriaList("PublishBookId", publishBookId), tx);    }
        #endregion

        #region ToXml
        protected override void ToXml_Autogenerated(System.Xml.XmlWriter w)
        {
            Store(w, "PublishId", this.PublishId);
            Store(w, "PublishBookId", this.PublishBookId);
            Store(w, "PublishToken", this.PublishToken);
            Store(w, "PublishStarted", this.PublishStarted);
            Store(w, "PublishFinished", this.PublishFinished);
            Store(w, "PublishTarget", this.PublishTarget);
        }
        #endregion

    }
}
